{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "bc6e0546-540a-4ecb-9399-387ed800f977",
   "metadata": {},
   "source": [
    "# 3、Numpy数组数据类型dtype"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "2951c655-811c-4ecc-b47f-db464dd0e809",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T19:15:31.805146Z",
     "iopub.status.busy": "2022-01-24T19:15:31.804856Z",
     "iopub.status.idle": "2022-01-24T19:15:31.947357Z",
     "shell.execute_reply": "2022-01-24T19:15:31.946401Z",
     "shell.execute_reply.started": "2022-01-24T19:15:31.805075Z"
    }
   },
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "6c9f7bd3-0927-48e1-be46-bc9b57025ee8",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T19:16:39.187123Z",
     "iopub.status.busy": "2022-01-24T19:16:39.186646Z",
     "iopub.status.idle": "2022-01-24T19:16:39.192617Z",
     "shell.execute_reply": "2022-01-24T19:16:39.191658Z",
     "shell.execute_reply.started": "2022-01-24T19:16:39.187088Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0 1 2 3 4 5 6 7 8]\n",
      "int64\n"
     ]
    }
   ],
   "source": [
    "# 查看数组数据类型\n",
    "a = np.arange(9)\n",
    "print(a)\n",
    "print(a.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "fd1ca1bb-3569-481c-8bc8-c2a692240b27",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T19:42:10.994053Z",
     "iopub.status.busy": "2022-01-24T19:42:10.993316Z",
     "iopub.status.idle": "2022-01-24T19:42:11.003058Z",
     "shell.execute_reply": "2022-01-24T19:42:11.002326Z",
     "shell.execute_reply.started": "2022-01-24T19:42:10.994025Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5]\n",
      "int8\n",
      "[1. 2. 3. 4. 5.]\n",
      "float16\n",
      "[<__main__.Person object at 0x10acf6f50>\n",
      " <__main__.Person object at 0x10acf74f0>]\n",
      "object\n",
      "[b'a' b'b']\n",
      "|S1\n",
      "['a' 'b']\n",
      "<U1\n"
     ]
    }
   ],
   "source": [
    "# 指定数据类型创建数组\n",
    "## 整型（以int8为例）\n",
    "b = np.array([1,2,3,4,5], dtype=np.int8)\n",
    "print(b)\n",
    "print(b.dtype)\n",
    "## 浮点型（以float16为例）\n",
    "c = np.array([1,2,3,4,5], dtype=np.float16)\n",
    "print(c)\n",
    "print(c.dtype)\n",
    "## Object类型\n",
    "class Person:\n",
    "    def __init__(self, name, age):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "d = np.array([Person(\"小王\", 13), Person(\"小张\", 15)])\n",
    "print(d)\n",
    "print(d.dtype)\n",
    "## 字符串类型（相当于python中的byte型，有编码）\n",
    "f = np.array(['a', 'b'], dtype=\"S\") #也写成 f = np.array(['a', 'b'], dtype=np.string_)\n",
    "print(f)\n",
    "print(f.dtype)\n",
    "## Unicode类型(未编码)\n",
    "e = np.array(['a', 'b'], dtype=\"U\")#也写成 f = np.array(['a', 'b'], dtype=np.unicode_)\n",
    "print(e)\n",
    "print(e.dtype)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "3000e09e-10be-4000-afe6-9d0e4a335f8a",
   "metadata": {
    "execution": {
     "iopub.execute_input": "2022-01-24T19:42:50.555854Z",
     "iopub.status.busy": "2022-01-24T19:42:50.555614Z",
     "iopub.status.idle": "2022-01-24T19:42:50.560451Z",
     "shell.execute_reply": "2022-01-24T19:42:50.559370Z",
     "shell.execute_reply.started": "2022-01-24T19:42:50.555832Z"
    },
    "tags": []
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "['a' 'b']\n",
      "<U1\n",
      "|S1\n"
     ]
    }
   ],
   "source": [
    "# 类型转换-.astype\n",
    "uf = f.astype(\"U\")\n",
    "print(uf)\n",
    "print(uf.dtype)\n",
    "print(f.dtype)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
